package cn.tedu.coolshark.controller;

import cn.tedu.coolshark.mapper.ProductMapper;
import cn.tedu.coolshark.pojo.dto.ProductInsertDto;
import cn.tedu.coolshark.pojo.entity.Product;
import cn.tedu.coolshark.pojo.vo.ProductAdminVO;
import cn.tedu.coolshark.pojo.vo.ProductUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductMapper mapper;

    @RequestMapping("/insert")
    public void insert(@RequestBody ProductInsertDto productInsertDto) {
        Product product = new Product();
        BeanUtils.copyProperties(productInsertDto, product);
        product.setCreated(new Date());
        product.setViewCount(0);
        mapper.insert(product);
    }

    @RequestMapping("select")
    public List<ProductAdminVO> select() {
        return mapper.selectForAdmin();
    }

    @RequestMapping("/delete")
    public void delete(int id) {
        String url = mapper.selectUrlById(id);
        new File("f:/files" + url).delete();
        mapper.delete(id);
    }

    @RequestMapping("/selectById")
    public ProductUpdateVO selectById(int id) {
        return mapper.selectById(id);
    }
}
